using System.Linq;
using Ninject;
using SDM.ExamPlanner.BLL.Interfaces;
using SDM.ExamPlanner.DALC.Interfaces;
using SDM.ExamPlanner.DTO;
using SDM.ExamPlanner.DTO.Abstract;

namespace SDM.ExamPlanner.BLL
{
    public class Manager<T> : IManager<T> where T : BaseEntity
    {
        private readonly IRepository<T> _repository = Ninjector.Kernel.Get<IRepository<T>>();

        public IQueryable<T> Find()
        {
            return _repository.Find();
        }

        public void Delete(T entity)
        {
            _repository.RegisterDeleted(entity);
            _repository.SaveChanges();
        }

        public void Save(T entity)
        {
            if (entity.Id == 0)
            {
                _repository.RegisterNew(entity);
            }
            else
            {
                _repository.RegisterUpdated(entity);
            }
            _repository.SaveChanges();
        }
    }
}
